import matplotlib
import pywt

matplotlib.use('TkAgg')
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 读取图像，以灰度模式
image = cv2.imread('../photo2.jpeg', cv2.IMREAD_GRAYSCALE)

# 进行单级小波分解
coeffs = pywt.dwt2(image, 'haar')
# 提取近似分量和细节分量
cA, (cH, cV, cD) = coeffs

# 小波重构
reconstructed_image = pywt.idwt2(coeffs, 'haar')

# 显示原始图像和重构图像
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.xticks([])
plt.yticks([])

plt.subplot(122)
plt.imshow(reconstructed_image, cmap='gray')
plt.title('Reconstructed Image')
plt.xticks([])
plt.yticks([])

plt.show()
